package com.c.ctgproject.ui.collect

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CarHandlerOverAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCarHandlerOverListBinding
import com.c.ctgproject.entity.guang.CarHandlerOverEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/9/9
 *描述:拆解单列表 页面
 */
class CarHandlerOverListActivity : BaseActivityV2(true), CarHandlerOverAdapter.CarHandlerOverAdapterListener {
    private val mBinding by lazy { ActivityCarHandlerOverListBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""
    private val mList = ArrayList<CarHandlerOverEntity.DataBean.ListBean>()
    private val mAdapter by lazy { CarHandlerOverAdapter(this, mList, this) }

    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1) {
            mList.removeAt(type.position)
            mAdapter.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)
    }

    //1-拆解中 2-拆解完成
//    private var dismantle = 1
    override fun click() {
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "拆解中" -> {
//                    dismantle = 1
                    searchMine = false
                    page = 1
                    getData()
                }
//                "已完成" -> {
//                    dismantle = 2
//                    page = 1
//                    getData()
//                }
                "我的" -> {
                    searchMine = true
//                    dismantle = 2
                    page = 1
                    getData()
                }
            }
        }


        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    private var searchMine = false

    private fun getData() {
        if (searchMine) {
            //流程类型 1-预录入 2-登记入库 3-过磅 4-初检 5-预处理 6-新建拆解 7-完成拆解 8-毁型拍照
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myCreateHandlerOrder(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.myHandlerCar(search, 7, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的车辆列表:$it")
                if (page == 1) mList.clear()
                val data = getGson().fromJson(it, CarHandlerOverEntity::class.java).data
                data?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })

        } else {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.carHandlerOverList(search, page, dismantle), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.carHandlerOverList(search, page, 1), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("拆解单列表:$it")
                val data = getGson().fromJson(it, CarHandlerOverEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })
        }
    }

    //dismantleMethod  1:毁型 2:拆配件
    override fun onHandlerCar(position: Int) {
        val data = mList[position]
        startActivity<CarHandlerPartActivity>(
            MyConstants.POSITION to position,
            MyConstants.ID to data.carDismantleId
        )
//        when  {
//            data.dismantleMethod==1 &&data.carType==4-> {
//                //五类车 直接拆
//                commonDialog("请再次确认拆解") {
//                    ApiMethods.apiSubscribe(ApiMethods.retrofit.carHandlerFinish(handlerBody("carDismantleId" to data.carDismantleId)), MyObserverHandlerLoading(this, loading) {
//                        MyLog.d("拆解完成回调:$it")
//                        showToast("拆解完成")
//                        page = 1
//                        getData()
//                    })
//                }
//            }
//            data.dismantleMethod== 1&&data.carType!=4->{
//                //毁型非五类车去添加配件
//                //拆配件
//                startActivity<CarHandlerPartActivity>(MyConstants.ID to data.carDismantleId)
//            }
//
//            data.dismantleMethod==2 -> {
//                //拆配件
//                startActivity<CarHandlerPartActivity>(MyConstants.ID to data.carDismantleId)
//            }
//            else -> {
////                startActivity<CarHandlerPartActivity>(MyConstants.ID to data.carDismantleId)
//                showToast("车辆数据异常")
//            }
//        }
    }

    override fun onUpLoadImage(position: Int) {

    }
}